package com.neusoft.elm.controller;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
 
import com.neusoft.elm.po.Food;
 import com.neusoft.elm.service.FoodService;
 import com.neusoft.elm.service.impl.FoodServiceImpl;
 
public class FoodController {
 
	public Object listFoodByBusinessId(HttpServletRequest request) throws Exception{
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        FoodService service = new FoodServiceImpl();
        List<Food> list = service.listFoodByBusinessId(businessId);
        return list;
    }
    
	// 为商家管理提供的新接口
    public Object listFoodForManage(HttpServletRequest request) throws Exception {
        try {
            Integer businessId = Integer.valueOf(request.getParameter("businessId"));
            String searchKeyword = request.getParameter("searchKeyword");
            String sortField = request.getParameter("sortField");
            int page = request.getParameter("page") != null ? 
                Integer.parseInt(request.getParameter("page")) : 1;
            int pageSize = request.getParameter("pageSize") != null ? 
                Integer.parseInt(request.getParameter("pageSize")) : 10;
                
            FoodService service = new FoodServiceImpl();
            Map<String, Object> result = service.listFoodForManage(
                businessId, searchKeyword, sortField, page, pageSize);
                
            return result;
        } catch (Exception e) {
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("code", 500);
            errorMap.put("message", "服务器错误: " + e.getMessage());
            return errorMap;
        }
    }
    
    public Object saveFood(HttpServletRequest request) throws Exception {
        try {
            Food food = new Food();
            food.setFoodName(request.getParameter("foodName"));
            food.setFoodExplain(request.getParameter("foodExplain"));
            food.setFoodImg(request.getParameter("foodImg"));
            food.setFoodPrice(Double.parseDouble(request.getParameter("foodPrice")));
            food.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
            food.setRemarks(request.getParameter("remarks"));
            
            FoodService service = new FoodServiceImpl();
            int result = service.saveFood(food);
            
            // 保持与updateFood一致的返回格式
            Map<String, Object> responseMap = new HashMap<>();
            responseMap.put("code", result > 0 ? 200 : 400);
            responseMap.put("message", result > 0 ? "添加成功" : "添加失败");
            responseMap.put("affectedRows", result);
            return responseMap;
        } catch (Exception e) {
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("code", 500);
            errorMap.put("message", "服务器错误: " + e.getMessage());
            return errorMap;
        }
    }
    
    public Object updateFood(HttpServletRequest request) throws Exception {
        try {
            Food food = new Food();
            food.setFoodId(Integer.valueOf(request.getParameter("foodId")));
            food.setFoodName(request.getParameter("foodName"));
            food.setFoodExplain(request.getParameter("foodExplain"));
            food.setFoodImg(request.getParameter("foodImg"));
            food.setFoodPrice(Double.parseDouble(request.getParameter("foodPrice")));
            food.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
            food.setRemarks(request.getParameter("remarks"));
            
            FoodService service = new FoodServiceImpl();
            int result = service.updateFood(food);
            
            // 返回更详细的结果信息
            Map<String, Object> responseMap = new HashMap<>();
            responseMap.put("code", result > 0 ? 200 : 400);
            responseMap.put("message", result > 0 ? "更新成功" : "更新失败");
            responseMap.put("affectedRows", result);
            return responseMap;
        } catch (Exception e) {
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("code", 500);
            errorMap.put("message", "服务器错误: " + e.getMessage());
            return errorMap;
        }
    }
    
    public Object getFoodById(HttpServletRequest request) throws Exception {
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        FoodService service = new FoodServiceImpl();
        return service.getFoodById(foodId);
    }
    
    public Object removeFood(HttpServletRequest request) {
        try {
            Integer foodId = Integer.valueOf(request.getParameter("foodId"));
            FoodService service = new FoodServiceImpl();
            int result = service.removeFood(foodId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", result > 0 ? 200 : 400);
            response.put("message", result > 0 ? "删除成功" : "删除失败");
            response.put("affectedRows", result);
            return response;
        } catch (Exception e) {
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("code", 500);
            errorMap.put("message", "服务器错误: " + e.getMessage());
            return errorMap;
        }
    }
    
    
 }